require 'libxml'
class RsqvpreviewController < ApplicationController
   
  def insert_datafield(node, tag)
     node << datafield = XML::Node.new("datafield")
     datafield['tag']=tag
     datafield['ind1']=" "
     datafield['ind2']=" "
     datafield
   end
 
   def insert_subfield(node, code, term)
     node << subfield = XML::Node.new("subfield")
     subfield['code']=code
     subfield.content=term
     subfield
   end
 
   def insert_controlfield(node, tag, term)
     node << controlfield = XML::Node.new("controlfield")
     controlfield['tag']=tag
     controlfield.content=term
     controlfield
   end
 
   def convert
     
     rismid="458002040"
          
     dn ='dn:http://www.loc.gov/MARC21/slim'
     xml_document = LibXML::XML::Document.file('/home/stephan/RSQV/cz.xml')
     if params[:extext]
       rismid=params[:extext]
     end
     book_xpath = '//dn:record/dn:controlfield[@tag="001"][contains(.,"'+rismid+'")]/..'
     nodes=xml_document.find(book_xpath,dn)
     @ids=xml_document.find('//dn:record/dn:controlfield[@tag="001"]/text()',dn).to_a.shuffle[0..6]
     @archive_node=Array.new
     nodes.each { |e|  @archive_node.push(e.copy(:deep => false))}
 
     doc = XML::Document.new()
     @doc1=doc
     doc.root = XML::Node.new("collection")
 
     #XML::Namespace.new(doc.root, nil, 'http://www.loc.gov/MARC21/slim')
 
     if nodes.size >= 1
       nodes.each { |item|
         doc.root << record=XML::Node.new("record")
         item.find('dn:leader',dn).each {|e| record<<leader=XML::Node.new("leader") ;  leader.content=e.content }
         item.find('dn:controlfield[@tag="001"]',dn).each {|e|  insert_controlfield(record, '001', e.content)}
         item.find('dn:controlfield[@tag="003"]',dn).each {|e|  insert_controlfield(record, '003', e.content)}
         item.find('dn:controlfield[@tag="005"]',dn).each {|e|  insert_controlfield(record, '005', e.content)}
         item.find('dn:controlfield[@tag="008"]',dn).each {|e|  insert_controlfield(record, '008', e.content)}
 
         item.find('dn:datafield[@tag="035"]',dn).each {  |e| df=insert_datafield(record, '035')
           e.find('dn:subfield[@code="a"]',dn).each {|e| insert_subfield(df,'a', e.content) }}
 
         item.find('dn:datafield[@tag="040"]',dn).each {  |e| df=insert_datafield(record, '040')
           e.find('dn:subfield[@code="a"]',dn).each {|e| insert_subfield(df,'a', e.content) }}
 
         item.find('dn:datafield[@tag="041"]',dn).each {  |e| df=insert_datafield(record, '041')
           e.find('dn:subfield[@code="a"]',dn).each {|e| insert_subfield(df,'a', e.content) }}
 
         item.find('dn:datafield[@tag="100"]',dn).each {  |e| df=insert_datafield(record, '100')
           e.find('dn:subfield[@code="a"]',dn).each {|e| insert_subfield(df,'a', e.content) }
           e.find('dn:subfield[@code="d"]',dn).each {|e| insert_subfield(df,'d', e.content) }
           e.find('dn:subfield[@code="0"]',dn).each {|e| insert_subfield(df,'0', e.content) }}
 
         item.find('dn:datafield[@tag="240"]',dn).each { |e| df=insert_datafield(record, '240')
           e.find('dn:subfield[@code="a"]',dn).each {|e|
             #Splitting title before <br>
             term=e.content
             stopchar = '<br>'
             term=term.sub /#{stopchar}.+/, ''
             insert_subfield(df,'a', term) }
           e.find('dn:subfield[@code="m"]',dn).each {|e|
             count=e.content.count('-')
             #Raw method for replacing many instruments
             if count>=5
               insert_subfield(df,'m', 'V(X), orch')
             else
               term=e.content
               term=term.gsub /Klv\./,'pf'
               term=term.gsub /Vl\./,'vl'
               term=term.gsub /Va\./,'vla'
               term=term.gsub /Vc\./,'vlc'
               term=term.gsub /Singst\./, 'V'
               insert_subfield(df,'m', term)
             end
           }
           #e.find('dn:subfield[@code="m"]',dn).each {|e| insert_subfield(df,'m', e.content) }
 
           e.find('dn:subfield[@code="n"]',dn).each {|e| insert_subfield(df,'n', e.content) }  }
 
         item.find('dn:datafield[@tag="245"]',dn).each {  |e| df=insert_datafield(record, '245')
           e.find('dn:subfield[@code="a"]',dn).each {|e| insert_subfield(df,'a', e.content) }}
 
         item.find('dn:datafield[@tag="260"]',dn).each {  |e| df=insert_datafield(record, '260')
           e.find('dn:subfield[@code="c"]',dn).each {|e| insert_subfield(df,'c', e.content) }}
 
         item.find('dn:datafield[@tag="300"]',dn).each {  |e| df=insert_datafield(record, '300')
           e.find('dn:subfield[@code="a"]',dn).each {|e| insert_subfield(df,'a', e.content) }
           e.find('dn:subfield[@code="c"]',dn).each {|e| insert_subfield(df,'c', e.content) }}
 
         item.find('dn:datafield[@tag="383"]',dn).each {  |e| df=insert_datafield(record, '383')
           e.find('dn:subfield[@code="b"]',dn).each {|e| insert_subfield(df,'b', e.content) }}
 
         item.find('dn:datafield[@tag="510"]',dn).each {  |e| df=insert_datafield(record, '510')
           e.find('dn:subfield[@code="a"]',dn).each {|e| insert_subfield(df,'a', e.content) }
           e.find('dn:subfield[@code="c"]',dn).each {|e| insert_subfield(df,'c', e.content) }}
 
         item.find('dn:datafield[@tag="592"]',dn).each {  |e| df=insert_datafield(record, '592')
           e.find('dn:subfield[@code="a"]',dn).each {|e| insert_subfield(df,'a', e.content) }}
         item.find('dn:datafield[@tag="593"]',dn).each {  |e| df=insert_datafield(record, '593')
           e.find('dn:subfield[@code="a"]',dn).each {|e| insert_subfield(df,'a', e.content) }}
         item.find('dn:datafield[@tag="594"]',dn).each {  |e| df=insert_datafield(record, '594')
           e.find('dn:subfield[@code="a"]',dn).each {|e| insert_subfield(df,'a', e.content) }}
         item.find('dn:datafield[@tag="595"]',dn).each {  |e| df=insert_datafield(record, '595')
           e.find('dn:subfield[@code="a"]',dn).each {|e| insert_subfield(df,'a', e.content) }}
 
         item.find('dn:datafield[@tag="700"]',dn).each {  |e| df=insert_datafield(record, '700')
           e.find('dn:subfield[@code="a"]',dn).each {|e| insert_subfield(df,'a', e.content) }
           e.find('dn:subfield[@code="d"]',dn).each {|e| insert_subfield(df,'d', e.content) }
           e.find('dn:subfield[@code="0"]',dn).each {|e| insert_subfield(df,'0', e.content) }
           e.find('dn:subfield[@code="4"]',dn).each {|e| insert_subfield(df,'4', e.content) }}
 
         item.find('dn:datafield[@tag="852"]',dn).each {  |e| df=insert_datafield(record, '852')
           e.find('dn:subfield[@code="a"]',dn).each {|e| insert_subfield(df,'a', e.content) }
           e.find('dn:subfield[@code="c"]',dn).each {|e| insert_subfield(df,'c', e.content) }
           e.find('dn:subfield[@code="x"]',dn).each {|e| insert_subfield(df,'x', e.content) }}
 
         item.find('dn:datafield[@tag="856"]',dn).each {  |e| df=insert_datafield(record, '856')
           e.find('dn:subfield[@code="u"]',dn).each {|e| insert_subfield(df,'u', e.content) }}
 
       }
     else
       @node="No result"
     end
 
   end
 end

